Minorfixes to "new" gtm_stringread() functions to eliminate leading and trailing...
authorRobert Lipe <robertlipe@users.noreply.github.com>
Sat, 17 Apr 2021 01:15:29 +0000 (20:15 -0500)
committerGitHub <noreply@github.com>
Sat, 17 Apr 2021 01:15:29 +0000 (20:15 -0500)
* Trim more uses of C date and potentially unsafe memory practices.

* Add warning fixes for garmin_xt and exif

dmtlog.cc
exif.cc
garmin_xt.cc
gtm.cc
vitosmt.cc

index 3b5597598646206f6dcd9170aeada389698ed631..7c338d5b07f5baac6e0eb22568b29b9e2479f7b0 100644 (file)
--- a/dmtlog.cc
+++ b/dmtlog.cc
@@ -33,6 +33,7 @@
 #include "defs.h"
 #include "gbfile.h"                     // for gbfgetdbl, gbfgetint32, gbfputint32, gbfgetint16, gbfputdbl, gbfputc, gbfread, gbfseek, gbfgetc, gbfile, gbfclose, gbfungetc, gbfeof, gbfputs, gbfwrite, gbfopen_le, gbfgetuint32, gbfputuint16, gbfputuint32
 #include "jeeps/gpsmath.h"              // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_C, GPS_Math_NGENToAiry1830LatLon
+#include "src/core/logging.h"           // for FatalMsg
 #include "xmlgeneric.h"                 // for cb_cdata, xg_callback, xg_string, xml_deinit, xml_init, cb_end, cb_start, xg_cb_type, xml_read, xml_readstring, xg_tag_mapping
 
 
@@ -329,7 +330,7 @@ tlog3b_xgcb_wpten(xg_string, const QXmlStreamAttributes*)
 }
 
 
-static char*
+static QString
 read_str(gbfile* f)
 {
   int i = gbfgetc(f);
@@ -337,13 +338,7 @@ read_str(gbfile* f)
     i = gbfgetint16(f);
   }
 
-  char* res = (char*) xmalloc(i + 1);
-  res[i] = '\0';
-  if (i) {
-    gbfread(res, 1, i, f);
-  }
-
-  return res;
+  return gbfreadbuf(i, f);
 }
 
 static void
@@ -380,17 +375,14 @@ write_str(const QString& str, gbfile* f)
 static int
 read_datum(gbfile* f)
 {
-  char* d = read_str(f);
-  char* g = read_str(f);
+  auto d = read_str(f);
+  auto g = read_str(f);
 
   int res = GPS_Lookup_Datum_Index(d);
 
-  if (*g && (strcmp(d, g) != 0)) {
-    fatal(MYNAME ": Unsupported combination of datum '%s' and grid '%s'!\n",
-          d, g);
+  if (d.compare(g))  {
+    fatal(FatalMsg() << MYNAME << ": Unsupported combination of datum '" << d << "' and grid '" << g << "''!\n");
   }
-  xfree(d);
-  xfree(g);
 
   return res;
 }
@@ -425,8 +417,7 @@ read_CTrackFile(const int version)
 
   /* S1 .. S9: comments, hints, jokes, aso */
   for (int i = 0; i < 9; i++) {
-    char* s = read_str(fin);
-    xfree(s);
+    (void) read_str(fin);
   }
 
   int32_t tcount = gbfgetint32(fin);
@@ -534,8 +525,8 @@ read_CTrackFile(const int version)
     // variants of shortname
 
     for (int32_t i = 0; i < namect; i++) {
-      char* name = read_str(fin);
-      if (name && *name) {
+      auto name = read_str(fin);
+      if (!name.isEmpty()) {
         switch (i) {
         case 0:
           wpt->description = name;
@@ -545,7 +536,6 @@ read_CTrackFile(const int version)
           break;
         }
       }
-      xfree(name);
     }
 
     waypt_add(wpt);
diff --git a/exif.cc b/exif.cc
index 124ff4edb55346cd4bee94da219e740ac0c3d6d9..9957938a95ff3a5dcb02013c921f2aa4a732058b 100644 (file)
--- a/exif.cc
+++ b/exif.cc
@@ -547,7 +547,7 @@ exif_read_ifd(ExifApp* app, const uint16_t ifd_nr, const gbsize_t offs,
         QByteArray qba(tag->count, 0);
         gbfread(qba.data(), tag->count, 1, fin);
         tag->data.append(qba);
-      } else for (uint16_t i = 0; i < tag->count; i++) {
+      } else for (unsigned i = 0; i < tag->count; i++) {
           switch (tag->type) {
           case EXIF_TYPE_SHORT:
           case EXIF_TYPE_SSHORT:
@@ -1318,7 +1318,7 @@ exif_write_ifd(ExifIfd* ifd, const char next, gbfile* fout)
     if (tag->size > 4) {
       if BYTE_TYPE(tag->type) {
         gbfwrite(tag->data.at(0).toByteArray().constData(), tag->size, 1, fout);
-      } else for (uint16_t i = 0; i < tag->count; i++) {
+      } else for (unsigned i = 0; i < tag->count; i++) {
           switch (tag->type) {
           case EXIF_TYPE_SHORT:
           case EXIF_TYPE_SSHORT:
index 5bc151c9647a35fa70d4a6a08b4a28873dbfa6ee..b98f9fb9460d6c53a6c2e19419a3c3430bf8100a 100644 (file)
@@ -151,7 +151,7 @@ format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color)
 static void
 format_garmin_xt_decrypt_trk_blk(int Count, uint8_t TrackBlock[])
 {
-  uint8_t j = 12;
+  int j = 12;
   while (j<(Count-1)) {
     for (uint8_t i = j; i < Count; i++) {
       TrackBlock[i] = TrackBlock[i] >> 1;
@@ -274,7 +274,7 @@ format_garmin_xt_proc_strk()
       format_garmin_xt_decrypt_trk_blk(Count, TrackBlock);
 
       // process each track point in the loaded TrackBlock
-      for (uint8_t ii = 1; ii <= ((Count-1) / 12); ii++) {
+      for (auto ii = 1; ii <= ((Count-1) / 12); ii++) {
         // decompose loaded track block part (track point)
         format_garmin_xt_decomp_trk_blk(ii, TrackBlock, &PrevEle, &Lat, &Lon, &Time);
 
diff --git a/gtm.cc b/gtm.cc
index 4fa1ef73a9ccc32a31372857d988d8dc15af24df..09615b8985d1e9b5136dc317e58193f2d69f624a 100644 (file)
--- a/gtm.cc
+++ b/gtm.cc
@@ -79,20 +79,7 @@ static QString
 fread_string(gbfile* fd)
 {
   int len = fread_integer(fd);
-
-  if (len == 0) {
-    return nullptr;
-  }
-
-  char* val = (char*) xmalloc(len+1);
-  gbfread(val, 1, len, fd);
-  while (len != 0 && val[len-1] == ' ') {
-    len--;
-  }
-  val[len] = 0;
-  QString v(val);
-  xfree(val);
-  return v;
+  return gbfreadbuf(len, fd);
 }
 
 static void
@@ -104,17 +91,7 @@ fread_string_discard(gbfile* fd)
 static QString
 fread_fixedstring(gbfile* fd, int len)
 {
-  char* val = (char*) xmalloc(len+1);
-
-  gbfread(val, 1, len, fd);
-  while (len != 0 && val[len-1] == ' ') {
-    len--;
-  }
-  val[len] = 0;
-  QString v(val);
-  xfree(val);
-
-  return v;
+  return gbfreadbuf(len, fd);
 }
 
 /* Write functions, according to specification. */
index 9823e857dc4e4324f87a10972d5e989c34fd8d80..fa9288e57baa94ff2c5b5becaaf71634479a620a 100644 (file)
@@ -69,11 +69,8 @@ static void
 vitosmt_read()
 {
   route_head* rte = nullptr;
-  struct tm tmStruct;
-  int  serial  =0;
+  int serial = 0;
 
-
-  memset(&tmStruct, 0, sizeof(tmStruct));
   /*
    * 24 bytes header
    */
@@ -128,20 +125,15 @@ vitosmt_read()
 
     auto* wpt_tmp = new Waypoint;
 
-    wpt_tmp->latitude =DEG(latrad);
-    wpt_tmp->longitude =DEG(lonrad);
-    wpt_tmp->altitude =elev;
+    wpt_tmp->latitude = DEG(latrad);
+    wpt_tmp->longitude = DEG(lonrad);
+    wpt_tmp->altitude = elev;
 
-    tmStruct.tm_year =timestamp[0]+100;
-    tmStruct.tm_mon =timestamp[1]-1;
-    tmStruct.tm_mday =timestamp[2];
-    tmStruct.tm_hour =timestamp[3];
-    tmStruct.tm_min =timestamp[4];
-    tmStruct.tm_sec  =(int)floor(seconds);
-    tmStruct.tm_isdst =-1;
+    wpt_tmp->SetCreationTime(QDateTime(
+      QDate(timestamp[0] + 2000, timestamp[1], timestamp[2]),
+      QTime(timestamp[3], timestamp[4]),
+      Qt::UTC).addMSecs(lround(seconds*1000.0)));
 
-    double usec = fmod(1000000*seconds+0.5,1000000);
-    wpt_tmp->SetCreationTime(mkgmtime(&tmStruct), lround(usec/1000.0));
     wpt_tmp->shortname = QString::asprintf("WP%04d", ++serial);
 
     WAYPT_SET(wpt_tmp, speed, KNOTS_TO_MPS(speed)); /* meters per second */